//
//  DMoreListViewController.swift
//  Drop
//
//  Created by hushaohua on 2018/2/7.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DMoreTableViewCell: UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DMoreListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white;
        self.setupTableView();
    }
    
    func setupTableView() -> Void{
        self.view.addSubview(self.tableView);
        self.registerTableViewCell();
    }
    
    func registerTableViewCell() -> Void{
        self.tableView.register(DMoreTableViewCell.self, forCellReuseIdentifier: "Cell");
    }
    
    lazy var sectionItems:[[DTableRowItem]] = {
        return self.tableRowSections();
    }();
    
    func tableRowSections() -> [[DTableRowItem]] {
        return [[DTableRowItem]]();
    }
    
    lazy var tableView:UITableView = {
        var tableView:UITableView = UITableView(frame:self.view.bounds, style:.grouped);
        tableView.delegate = self;
        tableView.dataSource = self;
        return tableView;
    }();
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sectionItems.count;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sectionItems[section].count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath);
        let item:DTableRowItem = self.sectionItems[indexPath.section][indexPath.row];
        cell.textLabel?.text = item.title;
        if (item.detail != nil){
            cell.detailTextLabel?.text = item.detail;
        }else{
            cell.detailTextLabel?.text = "";
        }
        cell.accessoryType = item.selected ? .checkmark : .none;
        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true);
        let item:DTableRowItem = self.sectionItems[indexPath.section][indexPath.row];
        if (!item.selected && item.closure != nil){
            item.closure!(indexPath);
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
